package org.tloss.web.editor.form.editors;

import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.model.IWorkbenchAdapter;
import org.eclipse.ui.views.properties.IPropertySource;
import org.tloss.web.editor.model.FormField;
import org.tloss.web.editor.utils.Constants;

public class FormFieldElement implements IWorkbenchAdapter, IAdaptable {
	private FormField field;
	private IResourceChangeListener dirtyListener;

	public FormFieldElement(FormField field,
			IResourceChangeListener dirtyListener) {
		this.field = field;
		this.dirtyListener = dirtyListener;
	}

	public FormField getField() {
		return field;
	}

	public void setField(FormField field) {
		this.field = field;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Object getAdapter(Class adapter) {

		if (adapter == IWorkbenchAdapter.class)
			return this;
		if (adapter == IPropertySource.class)
			return new FormFieldProperties(field, dirtyListener);
		return null;
	}

	@Override
	public Object[] getChildren(Object arg0) {
		return Constants.EMPTY_ARRAY;
	}

	@Override
	public ImageDescriptor getImageDescriptor(Object arg0) {
		return Constants.FIELD_DESCRIPTOR;
	}

	@Override
	public String getLabel(Object arg0) {
		return field.getName() != null ? field.getName() : "";
	}

	@Override
	public Object getParent(Object arg0) {
		return null;
	}

}
